//
//  moneyCountCell.swift
//  压岁_IOS
//
//  Created by 梓简王 on 2017/7/17.
//  Copyright © 2017年 梓简王. All rights reserved.
//

import Foundation

class moneyCountCell: UITableViewCell {
    var leftLabel:UILabel?
    var rightLabel:UILabel?
    var leftPic:UIImageView?
    var rightPic:UIImageView?
    var middlePic:UIImageView?
    var middlePoint:UIImageView?
    
    var flag:Int?//用于判断创建左视图还是右视图
    
    var model:commonAccountRecord?
    
    let labelWidth:CGFloat = commonVariable.SYSTEM_WIDTH/2 - CGFloat(commonVariable.TIMELINE_HEADER_WEIGTH + 10 - commonVariable.TIMELINE_CENTERPOINT_HEIGHT)
    
    required init?(coder aDecoder:NSCoder) {
        super.init(coder: aDecoder)
    }
    
    init(style:UITableViewCellStyle, reuseIdentifier:String?,index:Int,model:commonAccountRecord) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.flag = index
        self.model = model
        self.setUpUI()
    }
    
    func setUpUI(){
        //背景透明
        self.backgroundColor = UIColor.clearColor()
        self.selectionStyle = .None
        //初始化中间时间轴
        self.middlePic = UIImageView(frame: CGRect(x: 0, y: 0, width: commonVariable.TIMELINE_LINE_WIDTH, height: commonVariable.CELL_HEIGHT))
        self.middlePic?.backgroundColor = commonVariable.TIMELINE_LINE_COLOR
        self.middlePic?.center = CGPoint(x: commonVariable.SYSTEM_WIDTH/2, y: 22)
        //初始化中间时间轴点
        self.middlePoint = UIImageView(frame: CGRect(x: 0, y: 0, width: commonVariable.TIMELINE_CENTERPOINT_HEIGHT, height: commonVariable.TIMELINE_CENTERPOINT_HEIGHT))
        self.middlePoint?.backgroundColor = commonVariable.TIMELINE_CENTERPOINT_COLOR
        self.middlePoint?.layer.cornerRadius = CGFloat(commonVariable.TIMELINE_CENTERPOINT_HEIGHT/2)
        self.middlePoint?.clipsToBounds = true
        self.middlePoint?.center = CGPoint(x: commonVariable.SYSTEM_WIDTH/2, y: 22)
        //创建主视图
        if(self.model?.operationUserRole == "2"){
            self.createRightView()
        }else{
            self.createLeftView()
        }
        self.addSubview(self.middlePic!)
        self.addSubview(self.middlePoint!)
    }
    func createLeftView(){
        //创建头像
        self.leftPic = UIImageView(frame: CGRect(x: 0, y: 0, width: commonVariable.TIMELINE_HEADER_WEIGTH, height: commonVariable.TIMELINE_HEADER_WEIGTH))
        self.leftPic?.center = CGPoint(x: commonVariable.TIMELINE_HEADER_WEIGTH/2 + 10, y: commonVariable.CELL_HEIGHT/2)
        self.leftPic?.image = (self.model?.operationUserRole == "0") ? UIImage(named: "man") : UIImage(named: "woman")
        //创建标签
        let leftLabelRect = CGRect(x: CGFloat(commonVariable.TIMELINE_HEADER_WEIGTH + 10), y: CGFloat(commonVariable.CELL_HEIGHT/4), width: labelWidth, height: CGFloat(commonVariable.CELL_HEIGHT/2))
        self.leftLabel = UILabel(frame: leftLabelRect)
        //根据角色判断颜色
        self.leftLabel?.backgroundColor = (self.model?.operationUserRole == "0") ? commonVariable.PAPACOLOR : commonVariable.MAMACOLOR
        self.leftLabel?.layer.cornerRadius = (self.leftLabel?.bounds.height)!/2
        self.leftLabel?.clipsToBounds = true
        self.leftLabel?.text = (self.model?.recordRemark)!
        self.leftLabel?.font = UIFont.init(name: commonVariable.CHINAFONTNAME, size: 12)
        self.leftLabel?.textColor = commonVariable.IRONCOLOR
        self.leftLabel?.textAlignment = .Left
        self.addSubview(self.leftLabel!)
        self.addSubview(self.leftPic!)
    }
    func createRightView(){
        //创建头像
        self.rightPic = UIImageView(frame: CGRect(x: 0, y: 0, width: commonVariable.TIMELINE_HEADER_WEIGTH, height: commonVariable.TIMELINE_HEADER_WEIGTH))
        self.rightPic?.center = CGPoint(x: Int(self.bounds.width) - (commonVariable.TIMELINE_HEADER_WEIGTH/2 + 10), y: commonVariable.CELL_HEIGHT/2)
        self.rightPic?.image = UIImage(named: "baby")
        //创建标签
        self.rightLabel = UILabel(frame: CGRect(x: commonVariable.SYSTEM_WIDTH/2 - CGFloat(commonVariable.TIMELINE_CENTERPOINT_HEIGHT), y: CGFloat(commonVariable.CELL_HEIGHT/4), width: self.labelWidth, height: CGFloat(commonVariable.CELL_HEIGHT/2)))
        //根据角色判断颜色
        self.rightLabel?.backgroundColor = commonVariable.BABYCOLOR
        self.rightLabel?.layer.cornerRadius = (self.rightLabel?.bounds.height)!/2
        self.rightLabel?.clipsToBounds = true
        self.rightLabel?.text = (self.model?.recordRemark)!
        self.rightLabel?.font = UIFont.init(name: commonVariable.CHINAFONTNAME, size: 12)
        self.rightLabel?.textColor = commonVariable.IRONCOLOR
        self.rightLabel?.textAlignment = .Right
        self.middlePoint?.backgroundColor = commonVariable.DEEPREDCOLOR
        self.addSubview(self.rightLabel!)
        self.addSubview(self.rightPic!)
    }
}